from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication
from recordVideoUi import  Ui_MainWindow
from luping import recordVideo
from trayclass import TrayModel
import sys,time,json,os
from datetime import datetime
class recordWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(recordWindow, self).__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.recordAllScreen_slot)
        self.recordVideo = recordVideo()
        self.tray = TrayModel(self)
        self.tray.show()
        self.tray.stop_action.triggered.connect(self.stoprecordAllScreen_slot)

    def stoprecordAllScreen_slot(self):
        self.recordVideo.stoprecord()

    def recordAllScreen_slot(self):
        desktop = QApplication.desktop()
        height = desktop.height()
        width =desktop.width()
        self.setVisible(False)
        taskjson = {
            "result": "true",
            "data": {
                "id": "1",
                "width": width,
                "recordUrl": "Example Domain",
                "height": height,
                "audio": "",
                "bitrate": "1024k"
            }
        }
        self.recordVideo.taskParam['position'] = {}
        self.recordVideo.taskParam['position']['width'] = taskjson['data']['width']
        self.recordVideo.taskParam['position']['height'] = taskjson['data']['height']
        self.recordVideo.taskParam['allow_record'] = True
        self.recordVideo.taskParam['recordTime'] =0
        self.recordVideo.taskParam['file_path'] = os.path.join('recordVideoFile', datetime.now().strftime('%Y%m%d'))
        self.recordVideo.taskParam['timeStamp'] = str(int(time.time() * 1e3))
        self.recordVideo.taskParam['fileName'] = os.path.join(self.recordVideo.taskParam['file_path'],self.recordVideo.taskParam['timeStamp'] + ".mp4")
        self.recordVideo.taskParam['audioUrl'] = taskjson['data']['audio']
        self.recordVideo.taskParam['bitrate'] = taskjson['data']['bitrate']
        self.recordVideo.taskParam['recordId'] = taskjson['data']['id']
        self.tray.stop_action.setDisabled(False)
        self.tray.start_action.setDisabled(True)
        #self.recordVideo.taskParam =taskjson
        self.recordVideo.record_screen_by_user()
        print("record screen")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    myshow = recordWindow()
    myshow.show()
    sys.exit(app.exec_())